package sdfi.lee.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sdfi.lee.DAO.QuestionDAO;
import sdfi.lee.Question.Relation;

/**
 * Servlet implementation class GetProfileRelation
 */
public class GetProfileRelation extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetProfileRelation() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
			String profileid=request.getParameter("ID");
			List<Relation> list=new QuestionDAO().getRelation(profileid);
			StringBuffer bs=new StringBuffer();
			if(list!=null&&list.size()>0)
			{
				bs.append("[");
				for(Relation r:list)
				{
					bs.append("{\"entity\":\"");
					bs.append(r.getEntityName()+"\",\"profileid\":\"");
					bs.append(r.getProfileid()+"\",\"id\":\"");
					bs.append(r.getId()+"\"}");
					bs.append(",");
				}
				response.setCharacterEncoding("utf-8");
				PrintWriter pw=((HttpServletResponse)response).getWriter();
				pw.append(bs.toString().substring(0,bs.toString().lastIndexOf(",")));
				pw.append("]");
				pw.flush();
			}
			
			
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}

}
